<?php

namespace app\model;

use think\Exception;

class User
{
    private $connection=null;
    private $table_name='CONSLE_USER_TABLE_';

    public function __construct(){
        $serverName="27.115.111.58,14331";
        $user="sa";
        $password='Admin@123';
        $database='DSEDB';

        try {
            $connect=new \PDO("odbc:Driver={SQL Server};Server={$serverName};Database={$database}",$user,$password);
        }catch (\Exception $e){
            throw new Exception('连接数据库失败。'.$e->getMessage());
        }

        $this->connection=$connect;

    }

    public function query($query,$all=true){

        $result=$this->connection->query($query);
//var_dump($result);
        if($result){
            if($all){
                return $result->fetchAll();
            }
            return $result->fetch();
        }
        else{
            throw new Exception('语句执行出错。'.$query);
        }
    }

    public function find($query){
        return $this->query($query,false);
    }

    public function findUser($username,$password=''){
        $query="select * from CONSLE_USER_TABLE_ where Name='{$username}'";

        if($password){
            $query.=" and Pwd='{$password}'";
        }

        return $this->find($query);
    }

    public function register($username,$password){
        $query="insert into CONSLE_USER_TABLE_  (Name,Pwd) VALUES ('{$username}','{$password}')";


        return $this->query($query);
    }
}